﻿///************** google maps API scripts *****************/
var gmMap; // The global map constant.
var gmLAT = 39.75; // the global latitude (defaults to healthgrades location).
var gmLONG = -105; // the global longitude (defaults to healthgrades location).
var ZOOM_INIT = 14; // the global zoom amount.
var Markers = null;
var CurrentData = null;

$(document).ready(function () {
    Initialize();
    GetMapData();
});

function GetMapData() {
    var serviceUrl = "../service/cService.ashx?handler=LocationHandler&action=GetLocationPoints";
    $.ajax({
        url: serviceUrl,
        dataType: 'json',
        beforeSend: function () {
            // Handle the beforeSend event
        },
        success: GetMapDataSuccess,
        error: GetMapDataFailure
    }); // $.ajax
}

function GetMapDataFailure(XMLHttpRequest, textStatus, errorThrown) {
    alert("failure")
}

function GetMapDataSuccess(data, textStatus, xmlRequest) {
    CurrentData = new Array();
    CurrentData = data;
    for (var i = 0; i < data.length; i++) {
        data[i].ItemIndex = i + 1;
        AddMarkerPoint(data[i]);
    }

    $("#ResultTemplate").tmpl(data).appendTo("#side_results_container");

    $(".actionButton").click(function (e) {
        e.preventDefault();
        var itemIndex = $(this).attr("itemindex") - 1;
        GEvent.trigger(Markers[itemIndex], 'click');
    });

//    $("#ad_space_map").mouseenter(function () {
//        $(this).css("opacity, filter", ".8, alpha(opacity=80)");
//    });

//    $("#ad_space_map").mouseleave(function () {
//        $(this).css("opacity, filter", ".6, alpha(opacity=60)");
//    });
}

function Initialize() {
	if (GBrowserIsCompatible()) {
		gmMap = new GMap2(document.getElementById("map_canvas"));
		gmMap.setCenter(new GLatLng(gmLAT, gmLONG), ZOOM_INIT);
		gmMap.addControl(new GSmallMapControl());
		gmMap.addControl(new GMapTypeControl());
		gmMap.addControl(new GScaleControl());	
	}
}

function AddMarkerPoint(data) {

    var blueIcon = new GIcon(G_DEFAULT_ICON);
    blueIcon.image = "http://maps.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png";
    markerOptions = { icon: blueIcon, iconSize: new GSize(32, 32) };

    var latlng = new GLatLng(data.Latitude, data.Longitude);
    var marker = new GMarker(latlng, markerOptions);

    if (Markers == null) {
        Markers = new Array();
    }
    GEvent.addListener(marker, "click", function () {
        marker.openInfoWindowHtml(data.LocationName);
        UpdateMaterial(data.ItemIndex -1);
    });

    Markers.push(marker);
	gmMap.addOverlay(marker);
}

function UpdateMaterial(index) {
    if (!$("#top_panel").is(":visible")) {
        $('#top_panel').slideDown();
    }
    $(".HeaderText").text(CurrentData[index].LocationName);
}